package com.cskaoyan.wordmemorize.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.read.listener.ReadListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cskaoyan.wordmemorize.common.constant.ResultCodeEnum;
import com.cskaoyan.wordmemorize.common.exception.ExcelParseException;
import com.cskaoyan.wordmemorize.dao.entity.VocDO;
import com.cskaoyan.wordmemorize.dao.mapper.VocMapper;
import com.cskaoyan.wordmemorize.excel.FillExcelData;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class FillExcelValidListener extends AnalysisEventListener<FillExcelData> {

    VocMapper vocMapper;

    public FillExcelValidListener(VocMapper vocMapper) {
        this.vocMapper = vocMapper;
    }
    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
        if(headMap.size()!=7) throw new ExcelParseException(
                ResultCodeEnum.VOC_EXCEL_FORMAT_ERROR,
                ResultCodeEnum.VOC_EXCEL_FORMAT_ERROR.getMessage());
        if(!headMap.get(0).equals("填空题1")) throw new ExcelParseException(
                ResultCodeEnum.VOC_EXCEL_FORMAT_ERROR,
                ResultCodeEnum.VOC_EXCEL_FORMAT_ERROR.getMessage());
        if(!headMap.get(1).equals("答案1")) throw new ExcelParseException(
                ResultCodeEnum.VOC_EXCEL_FORMAT_ERROR,
                ResultCodeEnum.VOC_EXCEL_FORMAT_ERROR.getMessage());
        if(!headMap.get(2).equals("填空题2")) throw new ExcelParseException(
                ResultCodeEnum.VOC_EXCEL_FORMAT_ERROR,
                ResultCodeEnum.VOC_EXCEL_FORMAT_ERROR.getMessage());
        if(!headMap.get(3).equals("答案2")) throw new ExcelParseException(
                ResultCodeEnum.VOC_EXCEL_FORMAT_ERROR,
                ResultCodeEnum.VOC_EXCEL_FORMAT_ERROR.getMessage());
        if(!headMap.get(4).equals("填空题3")) throw new ExcelParseException(
                ResultCodeEnum.VOC_EXCEL_FORMAT_ERROR,
                ResultCodeEnum.VOC_EXCEL_FORMAT_ERROR.getMessage());
        if(!headMap.get(5).equals("答案3")) throw new ExcelParseException(
                ResultCodeEnum.VOC_EXCEL_FORMAT_ERROR,
                ResultCodeEnum.VOC_EXCEL_FORMAT_ERROR.getMessage());
        if(!headMap.get(6).equals("干扰项")) throw new ExcelParseException(
                ResultCodeEnum.VOC_EXCEL_FORMAT_ERROR,
                ResultCodeEnum.VOC_EXCEL_FORMAT_ERROR.getMessage());
    }
    @Override
    public void invoke(FillExcelData fillExcelData, AnalysisContext analysisContext) {
        List<String> vocS = new ArrayList<>();
        //检验是否有null
        if (fillExcelData.getSubTopicOne() == null) throw new ExcelParseException(
                ResultCodeEnum.PARAM_ERROR,
                "单元格中有空数据");
        if (fillExcelData.getSubTopicTwo() == null) throw new ExcelParseException(
                ResultCodeEnum.PARAM_ERROR,
                "单元格中有空数据");
        if (fillExcelData.getSubTopicThree() == null) throw new ExcelParseException(
                ResultCodeEnum.PARAM_ERROR,
                "单元格中有空数据");
        if (fillExcelData.getAnswerOne() == null) throw new ExcelParseException(
                ResultCodeEnum.PARAM_ERROR,
                "单元格中有空数据");
        else vocS.add(fillExcelData.getAnswerOne());
        if (fillExcelData.getAnswerTwo() == null) throw new ExcelParseException(
                ResultCodeEnum.PARAM_ERROR,
                "单元格中有空数据");
        else vocS.add(fillExcelData.getAnswerTwo());
        if (fillExcelData.getAnswerThree() == null) throw new ExcelParseException(
                ResultCodeEnum.PARAM_ERROR,
                "单元格中有空数据");
        else vocS.add(fillExcelData.getAnswerThree());
        if (fillExcelData.getDisturbAnswer() == null) throw new ExcelParseException(
                ResultCodeEnum.PARAM_ERROR,
                "单元格中有空数据");
        else vocS.add(fillExcelData.getDisturbAnswer());
        QueryWrapper<VocDO> vocDoQueryWrapper = new QueryWrapper<VocDO>();
        //检验单词是否存在
        for (String voc : vocS) {
            vocDoQueryWrapper.eq("content", voc);
            VocDO vocDO = vocMapper.selectOne(vocDoQueryWrapper);
            if (vocDO == null) {
                throw new ExcelParseException(
                        ResultCodeEnum.VOC_EXCEL_PARSE_ERROR,
                        "单词不存在");
            }
            vocDoQueryWrapper.clear();
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
